package com.admission.communicate;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import chinapay.Base64;

import com.admission.util.FileUtils;

public class KeyTools {

	public static String GenSignString(String orderID, String OrderAmount) throws Exception {
	
		chinapay.PrivateKey key=new chinapay.PrivateKey(); 
		chinapay.SecureLink t; 
		boolean flag; 
		String plainData = "";
		String ChkValue = "";
		
		String pathString = FileUtils.getProjectRoot()+"\\WEB-INF\\xml\\order.xml";
		SAXReader reader = new SAXReader();
		Document doc = reader.read(pathString); // 获取到xml文件
		Element rootElm = doc.getRootElement();
		
		String MerId = rootElm.element("MerId").getTextTrim();
		String CuryId = rootElm.element("CuryId").getTextTrim();
		String Version = rootElm.element("Version").getTextTrim();
		String BgRetUrl = rootElm.element("BgRetUrl").getTextTrim();
		String PageRetUrl = rootElm.element("PageRetUrl").getTextTrim();
		String ShareType = rootElm.element("ShareType").getTextTrim();
		String ShareAccount = rootElm.element("ShareAccount").getTextTrim();
		String ShareData = ShareAccount + "^" + OrderAmount;
 
//		String MerPrk = FileUtils.getProjectRoot()+"\\WEB-INF\\key\\MerPrK_808080121393586_20131213112216.key";
//		String MerPrk = "F:\\招生办源码1\\admissionoffice\\WebRoot\\WEB-INF\\key\\MerPrK_808080121393586_20131213112216.key";
		String MerPrk = rootElm.element("PrvKeyUrl").getTextTrim();
		
		flag=key.buildKey(MerId,0,MerPrk);
		if (flag==false) 
		{ 
			return "build key error!"; 
		} 
		
		t=new chinapay.SecureLink (key); 
		plainData = MerId + orderID + OrderAmount + CuryId + Version + BgRetUrl + PageRetUrl + ShareType + ShareData;
		String plainDataBase64 = new String(Base64.encode(plainData.toString().getBytes())); 
		ChkValue = t.Sign(plainDataBase64) ;
		
		return ChkValue;
	}
	
	
	public static String CheckReturnToken(String MerId, String plainData, String retValue) throws Exception{
		chinapay.PrivateKey key=new chinapay.PrivateKey(); 
		chinapay.SecureLink t; 
		boolean flag,flag1; 
		
		String pathString = FileUtils.getProjectRoot()+"\\WEB-INF\\xml\\order.xml";
		SAXReader reader = new SAXReader();
		Document doc = reader.read(pathString); // 获取到xml文件
		Element rootElm = doc.getRootElement();
		
		String PubKey = rootElm.element("PubKeyUrl").getTextTrim();
		//flag=key.buildKey(MerId,0,PubKey);
		flag=key.buildKey("999999999999999",0,PubKey); 
		if (flag==false) 
		{ 
			return "build key error!"; 
		} 
		t=new chinapay.SecureLink(key); 
		flag1 = t.verifyAuthToken(plainData, retValue);  // retValue为ChinaPay应答传回的域段
		if(flag1) {
			return "success";
		}
		else{
			return "fail";
		}	
	}
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			//String string = GenSignString("123","175");
			String string = CheckReturnToken("999999999999999","175","123");
			System.out.println(string); 
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
